package ca.scotthyndman.as2haxe.dom;

import java.util.ArrayList;
import java.util.List;

import ca.scotthyndman.as2haxe.dom.visitor.IVisitor;

/**
 * Represents an expression surrounded by parenthesis.
 */
public class ASParenthesizedExpression extends ASExpression {

	/**
	 * The "expression" structural property of this node type.
	 * 
	 * @since 3.0
	 */
	public static final ASChildPropertyDescriptor EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASParenthesizedExpression.class, "expression", ASExpression.class,
			true);

	/**
	 * A list of property descriptors (element type:
	 * {@link ASPropertyDescriptor}), or null if uninitialized.
	 */
	private static final List<ASPropertyDescriptor> PROPERTY_DESCRIPTORS;

	static {
		List<Object> properyList = new ArrayList<Object>(2);
		createPropertyList(ASParenthesizedExpression.class, properyList);
		addProperty(EXPRESSION_PROPERTY, properyList);
		PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
	}

	/**
	 * Returns a list of structural property descriptors for this node type.
	 * Clients must not modify the result.
	 * 
	 * @return a list of property descriptors (element type:
	 *         {@link ASPropertyDescriptor})
	 */
	public static List<ASPropertyDescriptor> propertyDescriptors() {
		return PROPERTY_DESCRIPTORS;
	}

	//
	// ======== MEMBERS
	//

	private ASExpression expression;

	/**
	 * The default constructor.
	 * 
	 */
	public ASParenthesizedExpression(AST ast) {
		super(ast);
	}

	/**
	 * Creates a new parenthesized expression with a child expression.
	 * 
	 * @param child
	 *            The child expression
	 */
	public ASParenthesizedExpression(AST ast, ASExpression child) {
		this(ast);
		setExpression(child);
	}

	/**
	 * Returns the child expression.
	 * 
	 * @return The expression
	 */
	public ASExpression getExpression() {
		return expression;
	}

	/**
	 * Sets the child expression.
	 * 
	 * @param expression
	 *            The new expression
	 */
	public void setExpression(ASExpression expression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.expression;
		preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
		this.expression = expression;
		postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
	}

	//
	// ======== INTERNAL GETTERS / SETTERS
	//

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final ASTNode internalGetSetChildProperty(
			ASChildPropertyDescriptor property, boolean get, ASTNode child) {
		if (property == EXPRESSION_PROPERTY) {
			if (get) {
				return getExpression();
			} else {
				setExpression((ASExpression) child);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetChildProperty(property, get, child);
	}

	//
	// ======== OVERRIDES
	//

	@Override
	public String toString() {
		return "(" + getExpression().toString() + ")";
	}

	/**
	 * Visitor pattern.
	 */
	@Override
	public void accept(IVisitor as2Visitor, boolean recursive) {
		recursive = as2Visitor.visit(this);

		if (recursive) {
			getExpression().accept(as2Visitor, recursive);
		}
	}
}
